#include <AtomicPriorityQueue.h>
#include <AtomicQueue.h>
#include <Task.h>
#include <iostream>
#include "CoreThread.h"
#include "DaemonThread.h"
#include "IdleThread.h"
#include "Task.h"
#include "ThreadSafePrinter.h"
#include "threadTest.h"
#include "threadpool/ThreadPool.h"
#include <chrono>
#include <thread>

class MyFunctionObject
{
public:
    void operator()() const
    {
        // 函数对象的操作
        // 这里可以编写函数对象要执行的代码
        ThreadSafePrinter::print("Executing function object");
    }
};

auto myFunctionObject = []() {
    // 函数对象的操作
    // 这里可以编写函数对象要执行的代码
    ThreadSafePrinter::print("Executing function object");
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return 10;
};

int add(int a, int b)
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
    ThreadSafePrinter::print("add");
    return a + b;
}

int add1()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    ThreadSafePrinter::print("add1");
    return 5;
}

int main()
{

#if 0
    HTHREADPOOL_SPACENAME::ThreadPool m_ThreadPool;
    auto ret = m_ThreadPool.commit(add, 6, 6);
    ThreadSafePrinter::print("ret:%d\n", ret.get());
    auto ret2 = m_ThreadPool.commit(add1);
    ThreadSafePrinter::print("ret2:%d\n", ret2.get());
    auto ret3 = m_ThreadPool.commit(myFunctionObject);
    ThreadSafePrinter::print("ret3:%d\n", ret3.get());



    int  a    = 9;
    int  b    = 10;
    auto ret1 = m_ThreadPool.commit([=]() -> int {
        ThreadSafePrinter::print("lambda");
        return a + b;
    });


    ThreadSafePrinter::print("lambda = %d\r\n", ret1.get());
#endif


    MyFunctionObject                                                        functionObject;
    std::vector<std::unique_ptr<HTHREADPOOL_SPACENAME::CoreThread>>         poolThread;
    HTHREADPOOL_SPACENAME::AtomicQueue<HTHREADPOOL_SPACENAME::Task>         task1;
    HTHREADPOOL_SPACENAME::AtomicPriorityQueue<HTHREADPOOL_SPACENAME::Task> poolPriorityTaskQueue;
    // HTHREADPOOL_SPACENAME::CoreThread                                       thread(0, &task1, &poolThread);
    // HTHREADPOOL_SPACENAME::IdleThread                                       thread1(&task1, &poolPriorityTaskQueue);

    HTHREADPOOL_SPACENAME::DaemonThread thread2(add1);
    // HTHREADPOOL_SPACENAME::DaemonThread thread2(std::bind(add1));
    // thread1.start();
    // thread.start();  // 启动线程
    thread2.start();
    std::this_thread::sleep_for(std::chrono::seconds(2));  // 等待一段时间
    // std::cout << "start....." << std::endl;
    #if 0



    std::cout << "start....." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));  // 让线程运行一段时间

    thread.stop();  // 暂停线程
    std::cout << "stop....." << std::endl;

    std::this_thread::sleep_for(std::chrono::seconds(5));  // 等待一段时间

    thread.start();
    std::cout << "start1....." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    thread.quit();
    std::cout << "quit....." << std::endl;
    thread.start();
    std::cout << "start2....." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));

    std::cout << "end....." << std::endl;
#endif
    return 0;
}
